<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\ReportRequest;
use App\Http\Resources\ReportResource;
use App\Models\Forum;
use App\Models\Topic;
use App\Repositories\ReportRepository;
use App\Repositories\TopicRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class ReportController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = Report::query();
        $q = $request->q;
        $clubId = $request->club_id;
        $technicianId = $request->technician_id;
        $with = $request->with;
        if ($with) {
            if (!is_array($with)) {
                $with = explode(',', $with);
            }
            $query->with($with);
        }
        if ($clubId) {
            $query->where('club_id', $clubId);
        }
        if ($technicianId) {
            $query->where('technician_id', $technicianId);
        }
        if ($q) {
            $q = '%' . $q . '%';
            $query->where(function ($query) use ($q) {
                $query->where('club_name', 'like', $q)->orWhere("club_short_name", "like", $q)
                    ->orWhere("technician_name", "like", $q)->orWhere("technician_short_name", "like", $q);
            });
        }

        $collection = $query->paginate($request->get('per_page', 10));

        $resource = ReportResource::collection($collection);

        return api(RET_OK, MSG_OK, $resource);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ReportRequest $request)
    {
        $user = Auth::user();
        $request->request->add(['type' => Topic::TYPE_REPORT]);
        $result = (new TopicRepository())->create($request, $user);
        return $result;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function config()
    {
        $forums = Forum::all(['name', 'id']);
        $topicTypes = [];
        foreach (Topic::$types as $type => $name) {
            $topicTypes[] = [
                'id' => $type,
                'text' => $name,
            ];
        }

        $out = compact('topicTypes', 'forums');
        return api(RET_OK, MSG_OK, $out);
    }
}
